/*  GStruct: data structure helpers for Java
 *  Copyright (C) 2011 Giulio Franco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package gstruct.extensions;

import java.util.Comparator;

/**
 * Utility functions for single data
 * @author Giulio Franco <giulio_franco@msn.com>
 *
 */
public final class Single {
	private Single() { }
	
	/**
	 * Returns the minimum between the two given parameters
	 * @param e1	First element
	 * @param e2	Second element
	 * @return			The element which is smaller, according to e1's comparison.
	 * 							Ties are broken arbitrarily.
	 */
	public static <E extends Comparable<? super E>> E min(E e1, E e2) {
		return (e1.compareTo(e2) < 0) ? e1 : e2;
	}
	
	/**
	 * Returns the minimum between the two given parameters
	 * @param e1	First element
	 * @param e2	Second element
	 * @param comparator External comparator
	 * @return			The element which is smaller, according to the given comparator.
	 * 							Ties are broken arbitrarily.
	 */
	public static <E> E min(E e1, E e2, Comparator<? super E> comparator) {
		return (comparator.compare(e1, e2) < 0) ? e1 : e2;
	}
	
	/**
	 * Returns the maximum between the two given parameters
	 * @param e1	First element
	 * @param e2	Second element
	 * @return			The element which is bigger, according to e1's comparison.
	 * 							Ties are broken arbitrarily.
	 */
	public static <E extends Comparable<? super E>> E max(E e1, E e2) {
		return (e1.compareTo(e2) > 0) ? e1 : e2;
	}
	
	/**
	 * Returns the maximum between the two given parameters
	 * @param e1	First element
	 * @param e2	Second element
	 * @param comparator External comparator
	 * @return			The element which is bigger, according to the given comparator.
	 * 							Ties are broken arbitrarily.
	 */
	public static <E> E max(E e1, E e2, Comparator<? super E> comparator) {
		return (comparator.compare(e1, e2) > 0) ? e1 : e2;
	}
}